package com.jt.common.util;

/**
 * Created by yc.feng in Intellij idea on 2017/3/20 0020/18:42
 */
public class NumberUtil {

    public static Integer parseInt(Object o) {
        if (o instanceof Number) {
            return ((Number) o).intValue();
        } else if (o instanceof String) {
            try {
                return Integer.parseInt((String) o);
            } catch (NumberFormatException e) {
                Double d = parseDouble(o);
                if (d != null)
                    return d.intValue();
            }
        }
        return null;
    }
    public static Long parseLong(Object o) {
        if (o instanceof Number) {
            return ((Number) o).longValue();
        } else if (o instanceof String) {
            try {
                return Long.parseLong((String) o);
            } catch (NumberFormatException e) {
                Double d = parseDouble(o);
                if (d != null)
                    return d.longValue();
            }
        }
        return null;
    }
    public static Double parseDouble(Object o) {
        if (o instanceof Number) {
            return ((Number) o).doubleValue();
        } else if (o instanceof String) {
            try {
                return Double.parseDouble((String) o);
            } catch (NumberFormatException e) {
            }
        }
        return null;
    }
    private NumberUtil() {

    }
}
